//
// Created by 32827 on 2024/7/13.
//
#include "yz_spi.h"
#include "yzl_spi_ads1220.h"

/*
 * a example init
 *  YZL_ADS1220_CFG_DEF def;
    YZF_MODIFY_REG(def.cfg0,YZL_ADS1220_CFG0_MUX_Msk,0b1000,YZL_ADS1220_CFG0_MUX_Pos);
    YZF_MODIFY_REG(def.cfg0,YZL_ADS1220_CFG0_GAIN_Msk,0b000,YZL_ADS1220_CFG0_GAIN_Pos);
    YZF_MODIFY_REG(def.cfg0,YZL_ADS1220_CFG0_PGA_BYPASS_Msk,0b0,YZL_ADS1220_CFG0_PGA_BYPASS_Pos);

    YZF_MODIFY_REG(def.cfg1,YZL_ADS1220_CFG1_DR_Msk,0b110,YZL_ADS1220_CFG1_DR_Pos);
    YZF_MODIFY_REG(def.cfg1,YZL_ADS1220_CFG1_MODE_Msk,0b10,YZL_ADS1220_CFG1_MODE_Pos);
    YZF_MODIFY_REG(def.cfg1,YZL_ADS1220_CFG1_CM_Msk,0b1,YZL_ADS1220_CFG1_CM_Pos);
    YZF_MODIFY_REG(def.cfg1,YZL_ADS1220_CFG1_TS_Msk,0b0,YZL_ADS1220_CFG1_TS_Pos);
    YZF_MODIFY_REG(def.cfg1,YZL_ADS1220_CFG1_BCS_Msk,0b0,YZL_ADS1220_CFG1_BCS_Pos);

    YZF_MODIFY_REG(def.cfg2,YZL_ADS1220_CFG2_VREF_Msk,0b00,YZL_ADS1220_CFG2_VREF_Pos);
    YZF_MODIFY_REG(def.cfg2,YZL_ADS1220_CFG2_50_60_Msk,0b00,YZL_ADS1220_CFG2_50_60_Pos);
    YZF_MODIFY_REG(def.cfg2,YZL_ADS1220_CFG2_PSW_Msk,0b00,YZL_ADS1220_CFG2_PSW_Pos);
    YZF_MODIFY_REG(def.cfg2,YZL_ADS1220_CFG2_IDAC_Msk,0b000,YZL_ADS1220_CFG2_IDAC_Pos);

    YZF_MODIFY_REG(def.cfg3,YZL_ADS1220_CFG3_I1MUX_Msk,0b000,YZL_ADS1220_CFG3_I1MUX_Pos);
    YZF_MODIFY_REG(def.cfg3,YZL_ADS1220_CFG3_I2MUX_Msk,0b000,YZL_ADS1220_CFG3_I2MUX_Pos);
    YZF_MODIFY_REG(def.cfg3,YZL_ADS1220_CFG3_DRDYM_Msk,0b0,YZL_ADS1220_CFG3_DRDYM_Pos);
    YZF_MODIFY_REG(def.cfg3,YZL_ADS1220_CFG3_RESERVED_Msk,0b0,YZL_ADS1220_CFG3_RESERVED_Pos);
 * */
YZF_RESULT yzl_ads1220_init(YZL_ADS1220_CFG_DEF *cfg,YZF_SPI_POINTER spi,YZL_GPIO cs3){
    yz_spi_recive_transmit_full_lines_start(spi);
    YLLOW(cs3);
    yzl_softport_delayUS(10);
    yz_spi_recive_transmit_full_lines(spi,YZL_ADS1220_RESET);
    yzl_softport_delayMS(1);
    yz_spi_recive_transmit_full_lines(spi, YZL_ADS1220_WREG(00,11));
    yz_spi_recive_transmit_full_lines(spi, cfg->cfg0);
    yz_spi_recive_transmit_full_lines(spi, cfg->cfg1);
    yz_spi_recive_transmit_full_lines(spi, cfg->cfg2);
    yz_spi_recive_transmit_full_lines(spi, cfg->cfg3);
    yz_spi_recive_transmit_full_lines(spi, YZL_ADS1220_RREG(00,11));
    uint8_t data1 = yz_spi_recive_transmit_full_lines(spi,0x0);
    uint8_t data2 = yz_spi_recive_transmit_full_lines(spi,0x0);
    uint8_t data3 = yz_spi_recive_transmit_full_lines(spi,0x0);
    uint8_t data4 = yz_spi_recive_transmit_full_lines(spi,0x0);
    if(data1!=cfg->cfg0||data2!=cfg->cfg1||data3!=cfg->cfg2||data4!=cfg->cfg3){
        return YZF_ERROR;
    }
    yz_spi_recive_transmit_full_lines(spi,YZL_ADS1220_START_SYNC);
    YLHIGH(cs3);
    yz_spi_recive_transmit_full_lines_end(spi);
    return YZF_OK;
}
uint32_t yzl_ads1220_read(YZF_SPI_POINTER spi,YZL_GPIO rdy,YZL_GPIO cs3){
    uint32_t Data;
    while (yzf_gpio_get(rdy.port,rdy.pin)==YZF_TRUE){

    }
//    yz_spi_recive_transmit_full_lines(spi,YZL_ADS1220_START_SYNC);
//    yz_spi_recive_transmit_full_lines(spi,YZL_ADS1220_RDATA);
    yz_spi_recive_transmit_full_lines_start(spi);
    YLLOW(cs3);
    yzl_softport_delayUS(10);
    uint8_t data2 = yz_spi_recive_transmit_full_lines(spi,0x0);
    uint8_t data3 = yz_spi_recive_transmit_full_lines(spi,0x0);
    uint8_t data4 = yz_spi_recive_transmit_full_lines(spi,0x0);
    yzl_softport_delayUS(10);
    YLHIGH(cs3);
    yz_spi_recive_transmit_full_lines_end(spi);
    Data = (data2 << 16) | (data3 << 8) | data4;
    return Data;
}
YZF_RESULT yzl_ads1220_sleep(YZF_SPI_POINTER spi,YZL_GPIO cs3){

    yz_spi_recive_transmit_full_lines_start(spi);
    YLLOW(cs3);
    yzl_softport_delayUS(10);
    yz_spi_recive_transmit_full_lines(spi,YZL_ADS1220_POWER_DOWN);
    yzl_softport_delayUS(10);
    YLHIGH(cs3);
    yz_spi_recive_transmit_full_lines_end(spi);

    return YZF_OK;
}
YZF_RESULT yzl_ads1220_wake(YZF_SPI_POINTER spi,YZL_GPIO cs3){

    yz_spi_recive_transmit_full_lines_start(spi);
    YLLOW(cs3);
    yzl_softport_delayUS(10);
    yz_spi_recive_transmit_full_lines(spi,YZL_ADS1220_START_SYNC);
    yzl_softport_delayUS(10);
    YLHIGH(cs3);
    yz_spi_recive_transmit_full_lines_end(spi);

    return YZF_OK;
}